package cn.chainco.cts.action;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import cn.chainco.cts.iservice.CommonManager;
import cn.chainco.cts.utils.query.OP;
import cn.chainco.cts.utils.query.Page;
import cn.chainco.cts.utils.query.QueryParam;
import cn.chainco.cts.iservice.IPointManager;
import cn.chainco.cts.jdo.Country;

import com.opensymphony.xwork2.ActionSupport;

public class CountryAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	public static final int BUFFER_SIZE = 2 * 1024;

	private CommonManager commonManager;
	private IPointManager pointManager;
	
	private int pageSize;
	private int pageNumber;
	private Page pager;
	private Country country;
	private List<Country> countryList;
	private String keyword;
	
	private File myFile;
	private String contentType;
	private String fileName;
	private String myFileContentType;
	private String myFileFileName;
	

	
	/**
	 * 模糊查询查国家
	 */
	@Action("searchCountryBykeyword")
	public String searchCountryBykeyword(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		List<QueryParam> queryList = new ArrayList<QueryParam>();
		QueryParam param = new QueryParam();
		param.setField("countryName like '%"+keyword+"%' or countryCode='"+keyword+"' group by countryName");
		param.setOp(OP.sql);
		param.setValue(new Object[]{});
		queryList.add(param);
		if(pageSize == 0)
			pageSize = 10;
		if(pageNumber == 0)
			pageNumber = 1;
		try {
			pager = this.commonManager.findPageByCustomized(pageNumber, pageSize, Country.class, queryList, null);
			countryList = pager.getCurrentPageElements();
		} catch (RuntimeException e1) {
			countryList = new ArrayList<Country>();
		}
		
		
		String sign = "no";
		Document document = DocumentHelper.createDocument();
		document.setXMLEncoding("UTF-8");
		Element rootElement = document.addElement("root");
		Element countNode = rootElement.addElement("count");
		countNode.addAttribute("count", "1");
		if(countryList != null && countryList.size() > 0){
			for(Country obj: countryList){
				Element searchObjNode = rootElement.addElement("searchObj");
				String value = obj.getCountryName();
				if( obj.getCountryCode()!=null && obj.getCountryCode().trim().length()>0 ){
					value +="("+obj.getCountryCode()+")";
				}
				if(value.indexOf("'") == -1){
					searchObjNode.addAttribute("value", value);
				}else{
					value = value.replaceAll("/'", "&acute;");
					searchObjNode.addAttribute("value", value);
				}
				searchObjNode.addAttribute("key", String.valueOf(obj.getId()));
				searchObjNode.addAttribute("type", "");
				searchObjNode.addAttribute("localName", "");
			}
			Element signNode = rootElement.addElement("signNode");
			signNode.addAttribute("sign", sign);
		}

		String outPutStr = document.asXML();
		try{
			Pattern pattern = Pattern.compile("\\([0-9]{4,10}\\)|\\*");
			Matcher m = pattern.matcher(outPutStr);
			outPutStr = m.replaceAll("");
		}catch(Exception e){

		}
		outputData(outPutStr);
		return null;
	}
	
//	 输出数据
	private void outputData(Object obj){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		try{
			response.getWriter().print(obj);
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public CommonManager getCommonManager() {
		return commonManager;
	}
	
	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public Page getPager() {
		return pager;
	}

	public void setPager(Page pager) {
		this.pager = pager;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public IPointManager getPointManager() {
		return pointManager;
	}

	public void setPointManager(IPointManager pointManager) {
		this.pointManager = pointManager;
	}
	
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContentType() {
		return myFileContentType;
	}

	public void setMyFileContentType(String myFileContentType) {
		this.myFileContentType = myFileContentType;
	}

	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public List<Country> getCountryList() {
		return countryList;
	}

	public void setCountryList(List<Country> countryList) {
		this.countryList = countryList;
	}

	public void setCommonManager(CommonManager commonManager) {
		this.commonManager = commonManager;
	}
	
}
